package itheima.d10;

import itheima.d09.Student;

import java.util.*;
import java.util.stream.Collectors;

public class Test {
    //终结方法
    public static void main(String[] args) {
        List<itheima.d09.Student> students = new ArrayList<>();
        students.add(new itheima.d09.Student("小王", 18, 66));
        students.add(new itheima.d09.Student("小张", 17, 99));
        students.add(new itheima.d09.Student("小李", 26, 20));
        students.add(new itheima.d09.Student("小赵", 16, 77));
        students.add(new itheima.d09.Student("小刘", 19, 18));



        //找出所有三个字的学生，统计个数
        long count = students.stream()
                .filter(s -> s.getName().length() == 3)
                .count();
        System.out.println("三个字的学生：" + count);

        //找出所有两个字学生的最大年龄

        Optional<Student> opt = students.stream()
                .filter(s -> s.getName().length() == 2)
                .max((s1, s2) -> s1.getAge() - s2.getAge());

        Student student = opt.get();
        System.out.println("年龄最大的学生：" + student);


        //找出成绩最后一名的 学生
        Optional<Student> opt2 = students.stream()
                .min((s1, s2) -> Double.compare(s1.getScrous(), s2.getScrous()));
        Student student1 = opt2.get();
        System.out.println("成绩最后一名的学生：" + student1);



        //找出所有不及格的学生，返回学生数组
        Student[] students1 = students.stream()
                .filter(s -> s.getScrous() >= 60)
                .toArray(len -> new Student[len]);

        for (Student student2 : students1) {
            System.out.println(student2);
        }



        //找出所有不及格的学生，返回学生集合
        List<Student> collect = students.stream()
                .filter(s -> s.getScrous() < 60)
                .collect(Collectors.toList());
        System.out.println(collect);


        Set<Student> collect1 = students.stream()
                .filter(s -> s.getScrous() < 60)
                .collect(Collectors.toSet());
        System.out.println(collect1);

        Map<String, Student> collect2 = students.stream()
                .filter(s -> s.getScrous() < 60)
                .collect(Collectors.toMap(Student::getName, s -> s));
        collect2.forEach((k, v) -> System.out.println(k + "--" + v));


    }
}
